﻿using System.Collections.Generic;
using System.Runtime.Serialization;
using GMME.Util;
using UnityEngine;
using UnityEngine.Scripting;

namespace GMME.Model.Rtm.Result
{
    [DataContract]
    public class GetRtmChannelInfoResult : ErrorResult
    {
        /**
         * 开发者传入的频道Id
         */
        [Preserve]
        [DataMember(Name = "channelId")]
        public string ChannelId { get; set; }

        /**
         * 频道内已订阅玩家数量
         */
        [Preserve]
        [DataMember(Name = "memberCount")]
        public int MemberCount { get; set; }

        /**
         * 频道内玩家列表
         */
        [Preserve]
        [DataMember(Name = "memberInfos")]
        public List<RtmChannelMemberInfo> MemberInfos { get; set; }

#if UNITY_ANDROID
        internal static GetRtmChannelInfoResult ConvertGetRtmChannelInfoResult(AndroidJavaObject javaObject)
        {
            if (javaObject != null)
            {
                GetRtmChannelInfoResult getRtmChannelInfoResult = new();
                getRtmChannelInfoResult.ChannelId = javaObject.Call<string>("getChannelId");
                getRtmChannelInfoResult.MemberCount = javaObject.Call<int>("getMemberCount");
                getRtmChannelInfoResult.Code = javaObject.Call<int>("getCode");
                getRtmChannelInfoResult.Msg = javaObject.Call<string>("getMsg");
                getRtmChannelInfoResult.MemberInfos = new List<RtmChannelMemberInfo>();
                var list = TransferUtil.JavaListToCsharpList(
                    javaObject.Call<AndroidJavaObject>("getMemberInfos"));
                foreach (var element in list)
                {
                    var rtmChannelMemberInfo =
                        RtmChannelMemberInfo.ConvertRtmChannelMemberInfo(element);
                    getRtmChannelInfoResult.MemberInfos.Add(rtmChannelMemberInfo);
                }

                return getRtmChannelInfoResult;
            }

            return null;
        }
#endif
    }
}